<? $this->head('css', 'assets/admin_categories'); ?>

<h1>Proizvodi</h1>


<p><?= $this->link('+Novi proizvod', '/admin_products/create', array(
    'remote' => true,
    'data' => array(
        'complete' => 'loadContent(req)'
    )
)) ?></p>
<?= $this->element('category_tree') ?>
<div class="left main">
    <?= $this->partial('products', array('data' => $data)) ?>
</div>

<script type="text/javascript">
$(function(){
    <? if(isset($category_id)): ?>
        treeSelect(<?= $category_id ?>);
    <? endif ?>
    $('.tree .label').click(function(){
        var id = $(this).parent().attr('value');
        
        var url = '/admin_products/index/'+id;
        location.hash = '!'+url;
        $.ajax({
            url: url,
            complete: loadContent
        })
    });

    locationHash(loadContent, function(url) {
        var id = false;
        if(cat = url.match(/index\/(\d+)$/)) {
            id = cat[1];
        }
        if(prod=url.match(/edit\/\d+\/(\d+)$/)) {
            id = prod[1];
        }

        if (id) {
            treeSelect(id);
        }
        return url;
    });
});

function loadContent(req) {
    throbberHide();
    $('.main.left').html(req.responseText);
}
</script>